package 代码随想录.数组;

public class a704二分查找 {
    static class Solution {
        public int search(int[] nums, int target) {
            int len = nums.length;
            int left = 0;
            int right = len - 1;

            while (left <= right) {
                int mid = ((right - left) >> 1) + left;
                if (nums[mid] > target) {
                    right = mid - 1;
                } else if (nums[mid] < target) {
                    left = mid + 1;
                } else {
                    return mid;
                }
            }
            return -1;
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {-1, 0, 3, 5, 9, 12};
        int target = 9;
        System.out.println(solution.search(nums, target));
    }
}
